package org.jeecg.modules.quartz.job;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import org.jeecg.common.nc.enums.WSEnum;
import org.jeecg.common.nc.service.NCWebService;
import org.jeecg.common.nc.vo.WSVO;
import org.jeecg.modules.nc.entity.NcOrg;
import org.jeecg.modules.nc.entity.NcSupfinance;
import org.jeecg.modules.nc.entity.NcSupplier;
import org.jeecg.modules.nc.service.impl.NcSupfinanceServiceImpl;
import org.jeecg.modules.nc.service.impl.NcSupplierServiceImpl;
import org.jeecg.modules.quartz.entity.TbJobLog;
import org.jeecg.modules.quartz.service.IQuartzJobService;
import org.jeecg.modules.quartz.service.ITbJobLogService;
import org.jeecg.modules.system.service.impl.SysDepartServiceImpl;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.List;
import java.util.Map;

public class NCSupplierSyncJob implements Job {

    private static final Logger logger = LoggerFactory.getLogger(NCUserSyncJob.class);

    @Autowired
    IQuartzJobService jobService;

    @Value("${qixin.ncserver.uri}")
    private String NC_SERVER_URI;

    @Autowired
    SysDepartServiceImpl sysDepartService;

    @Autowired
    NcSupfinanceServiceImpl ncSupfinanceService;

    @Autowired
    private ITbJobLogService tbJobLogService;

    @Autowired
    NcSupplierServiceImpl ncSupplierService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        // 获取组织个数
        List<NcOrg> ncOrgs = sysDepartService.querOrgList();

        try {
            String lastTime = jobService.queryLastTime("ncsupfinancesyncjob");
            String currentTime = DateUtil.now();
            Boolean first = false;
            if (StrUtil.isBlank(lastTime)) {
                lastTime = "2008-01-01 00:00:00";
                first = true; // 表示第一次同步
            }

            WSVO vo = new WSVO();
            if (ncOrgs.size() > 0) {
                // 循环组织名称更新供应商财务信息档案
                for (NcOrg ncOrg : ncOrgs) {
                    WSEnum.SUPFINANCE.setMsg(ncOrg.getCode());
                    vo = NCWebService.getFromWS(NC_SERVER_URI, WSEnum.SUPFINANCE, lastTime);
                    for (Map<String, Object> map : vo.getContent()) {
                        NcSupfinance ncSupfinance = new NcSupfinance();
                        ncSupfinance.setId((String) map.get("pk_supfinance")); // 供应商财务信息主键
                        ncSupfinance.setPkSupplier((String) map.get("pk_supplier")); //供应商主键
                        ncSupfinance.setPkOrg((String)map.get("pk_org")); //所属财务组织
                        ncSupfinanceService.saveOrUpdate(ncSupfinance);
                    }
                    TbJobLog tbJobLog = new TbJobLog();
                    tbJobLog.setName("NC供应商财务信息同步");
                    tbJobLog.setContent("本次新增或更新记录" + vo.getContent().size() + "条");
                    tbJobLog.setNum(vo.getContent().size());
                    tbJobLogService.save(tbJobLog);
                }
                    if(first){
                        // 新增一条记录
                        jobService.insetLastTime("ncsupfinancesyncjob", currentTime);
                    }else{
                        //更新最后同步时间
                        jobService.updateLastTime("ncsupfinancesyncjob", currentTime);
                    }
                    logger.info("NC供应商财务信息同步成功更新：{}条", vo.getContent().size());

            }
        }catch (Exception e){
            logger.info("NC供应商财务信息同步出现异常：{}", e.getMessage());
            e.printStackTrace();
            TbJobLog tbJobLog = new TbJobLog();
            tbJobLog.setName("NC供应商财务信息同步");
            tbJobLog.setContent("同步出现异常：" + e.getMessage());
            tbJobLog.setNum(0);
            tbJobLogService.save(tbJobLog);
        }


        try {
            String lastTime = jobService.queryLastTime("ncsuppliersyncjob");
            String currentTime = DateUtil.now();
            Boolean first = false;
            if (StrUtil.isBlank(lastTime)) {
                lastTime = "2008-01-01 00:00:00";
                first = true; // 表示第一次同步
            }

            WSVO vo = new WSVO();
            if (ncOrgs.size() > 0) {
                // 循环组织名称更新供应商信息档案
                for (NcOrg ncOrg : ncOrgs) {
                    WSEnum.SUPPLIER.setMsg(ncOrg.getCode());
                    vo = NCWebService.getFromWS(NC_SERVER_URI, WSEnum.SUPPLIER, lastTime);
                    for (Map<String, Object> map : vo.getContent()) {
                        NcSupplier ncSupplier = new NcSupplier();
                        ncSupplier.setId((String)map.get("pk_supplier"));
                        ncSupplier.setCode((String)map.get("code"));
                        ncSupplier.setEnablestate((Integer) map.get("enablestate"));
                        ncSupplier.setName((String)map.get("name"));
                        ncSupplier.setPkGroup((String)map.get("pk_group"));
                        ncSupplier.setPkOrg((String)map.get("pk_org"));

                        ncSupplierService.saveOrUpdate(ncSupplier);
                    }
                    TbJobLog tbJobLog = new TbJobLog();
                    tbJobLog.setName("NC供应商信息同步");
                    tbJobLog.setContent("本次新增或更新记录" + vo.getContent().size() + "条");
                    tbJobLog.setNum(vo.getContent().size());
                    tbJobLogService.save(tbJobLog);
                    if(first){
                        // 新增一条记录
                        jobService.insetLastTime("ncsuppliersyncjob", currentTime);
                    }else{
                        //更新最后同步时间
                        jobService.updateLastTime("ncsuppliersyncjob", currentTime);
                    }
                    logger.info("NC供应商信息同步成功更新：{}条", vo.getContent().size());
                }
            }
        }catch (Exception e){
            logger.info("NC供应商信息同步出现异常：{}", e.getMessage());
            e.printStackTrace();
            TbJobLog tbJobLog = new TbJobLog();
            tbJobLog.setName("NC供应商信息同步");
            tbJobLog.setContent("同步出现异常：" + e.getMessage());
            tbJobLog.setNum(0);
            tbJobLogService.save(tbJobLog);
        }

    }

}
